package com.zyh.controller;

import com.zyh.entity.HTable;
import com.zyh.entity.Student;
import com.zyh.service.HTableService;
import com.zyh.service.StudentService;
import com.zyh.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

@Controller
public class StudentController {
    @Resource
    StudentService studentService;
    @Resource
    UserService userService;
    @Resource
    HTableService hTableService;


    @RequestMapping("/sdoSave")
    public String sdoSave(@RequestParam("temperature")String temperature,@RequestParam("hStatus")String hstatus, Authentication authentication){
        Student student=studentService.findStudentBySno(Integer.parseInt(authentication.getName()));
        Date date=new Date();
        HTable hTable=new HTable();
        HTable oHTable=hTableService.findBySnoAndEnterTime(student.getSno(),date);
        if (oHTable==null){
            hTable.setSno(student.getSno());
            hTable.setTemperature(temperature);
            hTable.sethStatus(hstatus);
            hTable.setEnterTime(date);
            hTableService.save(hTable);
            return "studentView/saveSuccess";
        }else{
            oHTable.setTemperature(temperature);
            oHTable.sethStatus(hstatus);
            hTableService.save(oHTable);
            return "studentView/updateSuccess";
        }

    }
}
